<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%
    //保持keyword
    String keyword = request.getParameter("keyword") == null ? "" : request.getParameter("keyword");
    String pageNum = request.getParameter("pageNum") == null ? "" : request.getParameter("pageNum");
    String pageSize = request.getParameter("pageSize") == null ? "" : request.getParameter("pageSize");
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="/WEB-INF/include-head.jsp"%>
<link rel="stylesheet" href="static/css/pagination.css" />
<script type="text/javascript" src="static/jquery/jquery.pagination.js"></script>
<body>

<%@ include file="/WEB-INF/include-nav.jsp"%>
<div class="container-fluid">
    <div class="row">
        <%@ include file="/WEB-INF/include-sidebar.jsp"%>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <i class="glyphicon glyphicon-th"></i> 数据列表
                    </h3>
                </div>
                <div class="panel-body">
                    <form action="/admin/authority/user/list" method="post" id="searchForm" class="form-inline" role="form" style="float: left;">
                        <input type="hidden" id="pageNum" name="pageNum" value="<%=pageNum%>">
                        <input type="hidden" id="pageSize" name="pageSize" value="<%=pageSize%>">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input name="keyword" id="keyword" class="form-control has-success" type="text" value="<%=keyword %>" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="submit" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button type="button" class="btn btn-danger" style="float: right; margin-left: 10px;">
                        <i class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <a style="float: right;" href="admin/authority/user/add" class="btn btn-primary"><i class="glyphicon glyphicon-plus"></i> 新增</a>
                    <br>
                    <hr style="clear: both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input type="checkbox"></th>
                                <th>账号</th>
                                <th>名称</th>
                                <th>邮箱地址</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:if test="${empty requestScope.pageInfo.list}">
                                <tr>
                                    <td colspan="6" align="center">抱歉！没有查询到您要的数据！</td>
                                </tr>
                            </c:if>
                            <c:if test="${!empty requestScope.pageInfo.list}">
                                <c:forEach items="${requestScope.pageInfo.list}" var="admin" varStatus="myStatus">
                                    <tr>
                                        <td>${myStatus.count}</td>
                                        <td><input type="checkbox"></td>
                                        <td>${admin.loginAcct}</td>
                                        <td>${admin.userName}</td>
                                        <td>${admin.email}</td>
                                        <td>
                                            <a href="/admin/authority/role/assign/user?adminId=${admin.id}&pageNum=${requestScope.pageInfo.pageNum}&keyword=${param.keyword}" class="btn btn-success btn-xs"><i class=" glyphicon glyphicon-check"></i></a>
                                            <!-- 旧代码 -->
                                            <!-- <button type="button" class="btn btn-primary btn-xs">
                                                <i class=" glyphicon glyphicon-pencil"></i>
                                            </button> -->
                                            <!-- 新代码 -->
                                            <a href="admin/authority/user/update/${admin.id}?pageNum=${requestScope.pageInfo.pageNum}&keyword=${param.keyword}" class="btn btn-primary btn-xs"><i class=" glyphicon glyphicon-pencil"></i></a>
                                            <button type="button" class="btn btn-danger btn-xs" onclick="deleteUser(${admin.id})"><i class=" glyphicon glyphicon-remove"></i></button>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </c:if>
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="static/js/curl.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        <c:if test="${not empty requestScope.tips}">
            layer.alert("${requestScope.tips}")
        </c:if>
    });

    function initPagination() {
        var totalRecord = ${requestScope.pageInfo.total};
        var properties = {
            num_edge_entries: 3, // 边缘页数
            num_display_entries: 5, // 主体页数
            callback: pageSelectCallback, // 用户点击“翻页”按钮之后执行翻页操作的回调函数
            current_page: ${requestScope.pageInfo.pageNum - 1}, // 当前页，pageNum 从 1 开始，必须 -1 后才可以赋值
            prev_text: "上一页",
            next_text: "下一页",
            items_per_page: ${requestScope.pageInfo.pageSize}
        };

        $("#Pagination").pagination(totalRecord, properties);
    }

    function page(n, s) {
        $("#pageNum").val(n);
        $("#pageSize").val(s);
        $("#searchForm").submit();
        return false;
    }

    function pageSelectCallback(pageIndex, jQuery) {
        let pageNum = pageIndex + 1;
        page(pageNum, 10);
        return false;
    }
    $(function () {
        initPagination();
    });

    function deleteUser(id) {
        layer.confirm('确认删除？', {
            icon: 7,
            btn: ['删除', '取消']
        }, function () {
            let url = "admin/authority/user/delete/" + id;
            curl.post(url, "", undefined, true, function (msg) {
                if (msg.result === "success") {
                    showAlert("删除成功", 1, function () {
                        $("#searchForm").submit();
                    });
                } else {
                    showAlert(msg.message, 5);
                }
            }, showAlert);

        });
    }

    function showAlert(msg, icon, callback) {
        icon = icon || 1;
        layer.alert(msg, {icon: icon}, callback);
    }
</script>
</body>
</html>